package com.example.xgakj.mvp_demo.ui.activities;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.example.xgakj.mvp_demo.R;
import com.example.xgakj.mvp_demo.entity.PostInfo;
import com.example.xgakj.mvp_demo.manage.RetrofitManager;
import com.example.xgakj.mvp_demo.manage.net.RxSchedulers;
import com.example.xgakj.mvp_demo.utils.CommonUtil;
import com.example.xgakj.mvp_demo.utils.ToastUtil;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class RegisterActivity extends RxAppCompatActivity {
    @BindView(R.id.image_left)
    ImageView imageLeft;
    @BindView(R.id.ed_name)
    EditText edName;
    @BindView(R.id.ed_password)
    EditText edPassword;
    @BindView(R.id.ed_password2)
    EditText edPassword2;
    @BindView(R.id.btn_login)
    Button btnLogin;
    private ProgressDialog progressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        progressDialog=new ProgressDialog(this);
        progressDialog.setMessage("正在注册。。");
        progressDialog.setCanceledOnTouchOutside(false);
    }

    @OnClick({R.id.image_left, R.id.btn_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.image_left:
                finish();
                break;
            case R.id.btn_login:
                String user=edName.getText().toString().trim();
                String password1=edPassword.getText().toString().trim();
                String password2=edPassword2.getText().toString().trim();
                if (!checkEmpty(user,password1,password2)){
                    RetrofitManager.getInstance().getRegisterInfo(user,password1)
                    .compose(RxSchedulers.<PostInfo>compose())
                            .compose(this.<PostInfo>bindToLifecycle())
                    .doOnSubscribe(new Consumer<Disposable>() {
                        @Override
                        public void accept(@NonNull Disposable disposable) throws Exception {
                            if (CommonUtil.isNetworkAvailable()){
                                ToastUtil.showToast("网络连接不可用");
                                disposable.dispose();
                            }else {
                                progressDialog.show();
                            }
                        }
                    })
                    .subscribe(new Consumer<PostInfo>() {
                        @Override
                        public void accept(@NonNull PostInfo postInfo) throws Exception {
                            ToastUtil.showToast("注册成功");
                            progressDialog.dismiss();
                        }
                    },new Consumer<Throwable>(){

                        @Override
                        public void accept(@NonNull Throwable throwable) throws Exception {
                            ToastUtil.showToast("注册失败");
                            progressDialog.dismiss();
                        }
                    })
                    ;
                }
                break;
        }
    }

    private boolean checkEmpty(String user, String password1, String password2) {
        if (TextUtils.isEmpty(user)||TextUtils.isEmpty(password1)){
            ToastUtil.showToast("输入不能为空");
            return true;
        }else if (password1.length()<6||password1.length()>10){
            ToastUtil.showToast("密码长度必须为6-10位");
            return true;
        }else if (password1!=password2){
            ToastUtil.showToast("两次输入密码不一致，请重新输入");
            return true;
        }else {
            return false;
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (progressDialog!=null){
            progressDialog.cancel();

        }
    }
}
